package com.hogan.hoganproject.contacts;

import android.database.Cursor;
import android.provider.ContactsContract;

import com.hogan.hoganproject.base.app.HoganApplication;
import com.hogan.hoganproject.base.utils.LogUtil;
import com.hogan.hoganproject.contacts.adapter.ContactsItemBean;
import com.hogan.hoganproject.pinyin.PinyinComparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * Author: chenhu
 * Time: 2020/9/3 19:51
 * Description do somethings
 */
public class ContactsUtils {

    public ContactsUtils() {
    }

    private static class ContactsHolder {
        private static ContactsUtils INSTANCE = new ContactsUtils();
    }

    public static ContactsUtils getInstance() {
        return ContactsHolder.INSTANCE;
    }

    public List<ContactsItemBean> getAllContacts() {
        List<ContactsItemBean> mContactsItemBeanList = new ArrayList<>();
        Cursor cursor = HoganApplication.getInstance().getBaseContext()
                .getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                        null, null, null, null);
        while (cursor.moveToNext()) {
            ContactsItemBean contactsItemBean = new ContactsItemBean();
            String contactId = cursor.getString(cursor

                    .getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            contactsItemBean.setUserName(name);
            //获取联系人电话号码
            Cursor phoneCursor = HoganApplication.getInstance().getBaseContext().
                    getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
                    null, null);
            while (phoneCursor.moveToNext()) {
                String phone = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                phone = phone.replace("-", "");
                phone = phone.replace(" ", "");
                contactsItemBean.setUserPhone(phone);
            }
            mContactsItemBeanList.add(contactsItemBean);
        }
        cursor.close();
        mContactsItemBeanList.addAll(getData());
        LogUtil.w("mContactsItemBeanList.size===" + mContactsItemBeanList.size());

        Collections.sort(mContactsItemBeanList, new PinyinComparator<ContactsItemBean>() {
            @Override
            public int compare(ContactsItemBean s1, ContactsItemBean s2) {
                return compare(s1.getUserName(), s2.getUserName());
            }
        });
        return mContactsItemBeanList;
    }

    private List<ContactsItemBean> getData() {

        List<ContactsItemBean> items = new ArrayList<>();
        items.add(new ContactsItemBean("路平"));
        items.add(new ContactsItemBean("苏唐"));
        items.add(new ContactsItemBean("莫林"));
        items.add(new ContactsItemBean("燕西凡"));
        items.add(new ContactsItemBean("郭有道"));
        items.add(new ContactsItemBean("楚敏"));
        items.add(new ContactsItemBean("子牧"));
        items.add(new ContactsItemBean("秦桑"));
        items.add(new ContactsItemBean("秦琪"));
        items.add(new ContactsItemBean("冷休谈"));
        items.add(new ContactsItemBean("燕秋辞"));
        items.add(new ContactsItemBean("昭音初"));
        items.add(new ContactsItemBean("吕沉风"));
        items.add(new ContactsItemBean("林天表"));
        items.add(new ContactsItemBean("方倚注"));
        items.add(new ContactsItemBean("唐小妹"));
        items.add(new ContactsItemBean("孙迎升"));
        items.add(new ContactsItemBean("孙送招"));
        items.add(new ContactsItemBean("查梦良"));
        items.add(new ContactsItemBean("秦越"));
        items.add(new ContactsItemBean("严歌"));
        items.add(new ContactsItemBean("徐迈"));
        items.add(new ContactsItemBean("詹仁"));
        items.add(new ContactsItemBean("许唯风"));
        items.add(new ContactsItemBean("宋远"));
        items.add(new ContactsItemBean("李遥天"));
        items.add(new ContactsItemBean("陈楚"));
        items.add(new ContactsItemBean("霍英"));
        items.add(new ContactsItemBean("程落烛"));
        items.add(new ContactsItemBean("郭无术"));
        items.add(new ContactsItemBean("阮青竹"));
        items.add(new ContactsItemBean("严鸣"));
        return items;
    }

}
